
import unittest
import diabind
import testdata
import os

class SymFuncTest( unittest.TestCase ):

    def __get_glob(self):
        if not hasattr(self, "__glob"):
            self.__glob = testdata.loaded_session.get_globalScope()
        return self.__glob

    def __get_func(self, name):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    name,
                                                    diabind.nsfCaseSensitive )
        self.assertEqual(len(enum_syms), 1)
        return enum_syms[0]

    def test_classParent(self):
        self.assertEqual(self.__get_func("diabind::CLoader::FromPdb").get_classParent().get_name(), "diabind::CLoader")
        self.assertRaises( diabind.DiaError,
                           self.__get_func("DllMain").get_classParent )

    def test_access(self):
        self.assertEqual(self.__get_func("diabind::CLoader::FromPdb").get_access(), diabind.CV_public)
        self.assertEqual(self.__get_func("diabind::CLoader::FromPdb80").get_access(), diabind.CV_protected)
        self.assertRaises( diabind.DiaError,
                           self.__get_func("DllMain").get_access )

    def test_callingConvention(self):
        machine = self.__get_glob().get_machineType()
        self.assertTrue(machine in [diabind.ImageFileMachine_I386, diabind.ImageFileMachine_AMD64])
        if machine == diabind.ImageFileMachine_I386:
            self.assertEqual(self.__get_func("DllMain").get_type().get_callingConvention(), diabind.CV_CALL_NEAR_STD)
